//
//  WorkingPlanController.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 1/10/17.
//  Copyright © 2017 com.mask. All rights reserved.
//

import UIKit

class WorkingPlanController: UICollectionViewController {
    
    var data: [AnyObject]?
    let sectionInsets = UIEdgeInsets(top: Padding.top, left: Padding.left, bottom: Padding.top, right: Padding.left)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.alwaysBounceVertical = true
        collectionView?.backgroundColor = Color.bgGray
        collectionView?.register(WorkingPlanCell.self, forCellWithReuseIdentifier: "workingPlanCell")
        loadData()
    }
    
    func loadData() {
        data = [AnyObject]()
        for _ in 0...10 {
            data?.append("placeholderImage.jpg" as AnyObject)
        }
        collectionView?.reloadData()
    }
    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data?.count ?? 0
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "workingPlanCell", for: indexPath) as? WorkingPlanCell
        cell?.imageUrl = data?[indexPath.row] as! String?
        return cell!
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        var images = [SKPhoto]()
//        let count = data?.count ?? 0
//        for _ in 1...count {
//            let photo = SKPhoto.photoWithImage(UIImage(named: data?[indexPath.row] as! String)!)
//            images.append(photo)
//        }
//        let browser = SKPhotoBrowser(photos: images)
//        browser.initializePageIndex(indexPath.row)
//        present(browser, animated: true, completion: nil)
    }
}

extension WorkingPlanController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let paddingSpace = Padding.left * 2 + Padding.space * 3
        let availableWidth = view.frame.width - paddingSpace
        let widthPerItem = availableWidth / 4
        return CGSize(width: widthPerItem, height: widthPerItem)
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {
        return sectionInsets
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return Padding.space
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return Padding.space
    }
}

